package com.mcc.app.process.service.impl;

import java.util.List;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mcc.app.cache.service.IPlcCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mcc.app.process.mapper.SinterSiloMapper;
import com.mcc.app.process.domain.SinterSilo;
import com.mcc.app.process.service.ISinterSiloService;

/**
 * 料仓Service业务层处理
 *
 * @author gxy
 * @date 2025-06-20
 */
@Service
public class SinterSiloServiceImpl implements ISinterSiloService {

    @Autowired
    private SinterSiloMapper sinterSiloMapper;

    @Autowired
    private IPlcCacheService plcCacheService;

    /**
     * 查询料仓
     *
     * @param siloId 料仓ID
     * @return 料仓
     */
    @Override
    public SinterSilo selectSinterSiloById(Long siloId) {
        return sinterSiloMapper.selectById(siloId);
    }

    /**
     * 查询料仓列表
     *
     * @param sinterSilo 料仓
     * @return 料仓
     */
    @Override
    public List<SinterSilo> selectSinterSiloList(SinterSilo sinterSilo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (ObjectUtil.isNotEmpty(sinterSilo)) {
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getSiloCode()), "silo_code", sinterSilo.getSiloCode());
            queryWrapper.like(ObjectUtil.isNotEmpty(sinterSilo.getMatterName()), "matter_name", sinterSilo.getMatterName());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getWeightCode()), "weight_code", sinterSilo.getWeightCode());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getCuttingCode()), "cutting_code", sinterSilo.getCuttingCode());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getSetCode()), "set_code", sinterSilo.getSetCode());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getAccudCode()), "accud_code", sinterSilo.getAccudCode());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getAccunCode()), "accun_code", sinterSilo.getAccunCode());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getTotalCode()), "total_code", sinterSilo.getTotalCode());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getPeibiCode()), "peibi_code", sinterSilo.getPeibiCode());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getPianchaCode()), "piancha_code", sinterSilo.getPianchaCode());
            queryWrapper.eq(ObjectUtil.isNotEmpty(sinterSilo.getPreAdayCode()), "pre_aday_code", sinterSilo.getPreAdayCode());

        }
        List<SinterSilo> sinterSilos = sinterSiloMapper.selectList(queryWrapper);
        for(SinterSilo item : sinterSilos){
            //String matterNameCode = item.getMatterName();
            //String matterName = plcCacheService.getCacheDataToStr(item.getMatterName());
            item.setSiloCode(plcCacheService.getCacheDataToStr(item.getSiloCode()));
            item.setMatterName(plcCacheService.getCacheDataToStr(item.getMatterName()));
            item.setWeightCode(plcCacheService.getCacheDataToStr(item.getWeightCode()));
            item.setFrequencyCode(plcCacheService.getCacheDataToStr(item.getFrequencyCode()));
            item.setCuttingCode(plcCacheService.getCacheDataToStr(item.getCuttingCode()));
            item.setSetCode(plcCacheService.getCacheDataToStr(item.getSetCode()));
            item.setTotalCode(plcCacheService.getCacheDataToStr(item.getTotalCode()));
            item.setPeibiCode(plcCacheService.getCacheDataToStr(item.getPeibiCode()));
            item.setPianchaCode(plcCacheService.getCacheDataToStr(item.getPianchaCode()));
            item.setMidCode(plcCacheService.getCacheDataToStr(item.getMidCode()));
            item.setAccunCode(plcCacheService.getCacheDataToStr(item.getAccunCode()));
            item.setAccudCode(plcCacheService.getCacheDataToStr(item.getAccudCode()));
            item.setPreAdayCode(plcCacheService.getCacheDataToStr(item.getPreAdayCode()));

        }
        return sinterSilos;
    }

    /**
     * 新增料仓
     *
     * @param sinterSilo 料仓
     * @return 结果
     */
    @Override
    public int insertSinterSilo(SinterSilo sinterSilo) {
        return sinterSiloMapper.insert(sinterSilo);
    }

    /**
     * 修改料仓
     *
     * @param sinterSilo 料仓
     * @return 结果
     */
    @Override
    public int updateSinterSilo(SinterSilo sinterSilo) {
        return sinterSiloMapper.updateById(sinterSilo);
    }

    /**
     * 删除料仓
     *
     * @param siloId 料仓ID
     * @return 结果
     */
    @Override
    public int deleteSinterSiloById(Long siloId) {
        return sinterSiloMapper.deleteById(siloId);
    }

    /**
     * 批量删除料仓
     *
     * @param siloIds 料仓ID
     * @return 结果
     */
    @Override
    public int deleteSinterSiloByIds(Long[] siloIds) {
        if (siloIds.length == 0) return 0;
        return sinterSiloMapper.deleteBatchIds(ListUtil.toList(siloIds));
    }
}
